package com.nio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

/**
 * @author xl-9527
 * @since 2025/7/22
 **/
public class PathAndFilesDemo {

    private static final Logger log = LoggerFactory.getLogger(PathAndFilesDemo.class);

    public Path getPath() {
        return Path.of("./a.txt");
    }

    public boolean createFile() throws IOException {
        final Path path = this.getPath();
        if (!Files.exists(path)) {
            Files.createFile(path);
            return true;
        } else {
            log.warn("file already exists");
            return false;
        }
    }

    public void writeContent(String content) throws IOException {
        final Path path = this.getPath();
        if (this.createFile()) {
            Files.write(path, content.getBytes(StandardCharsets.UTF_8));
        } else
            Files.write(path, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
    }
}
